
#include "InetAddress.h"

InetAddress::InetAddress(uint16_t port, std::string ip) {
    bzero(&address_, sizeof(address_));
    address_.sin_family = AF_INET;
    address_.sin_addr.s_addr = INADDR_ANY;
    inet_pton(AF_INET, ip.c_str(), &address_.sin_addr);
    address_.sin_port = htons(port);
}

InetAddress::InetAddress(sockaddr_in address) : address_(address) {}

sockaddr_in InetAddress::Address() {
    return address_;
}

std::string InetAddress::Ip() {
    char buf[32] = {0};
    inet_ntop(AF_INET, &address_.sin_addr, buf, sizeof(buf));
    return std::string(buf);
}

uint16_t InetAddress::Port() {
    return ntohs(address_.sin_port);
}

std::string InetAddress::IpPort() {
    char buf[64] = {0};
    snprintf(buf, sizeof(buf), "%s:%u", Ip().c_str(), Port());
    return std::string(buf);
}

void InetAddress::SetAddress(struct sockaddr_in address) {
    address_ = address;
}
